package com.e3.basdoc.dept.server;

import java.util.ArrayList;
import java.util.List;

import com.e3.basdoc.dept.client.DeptHelper;
import com.e3.basdoc.dept.client.DeptRPCService;
import com.e3.basdoc.dept.model.DeptModel;
import com.e3.basdoc.dept.model.DeptQC;
import com.e3.basdoc.dept.service.DeptQueryService;
import com.e3.basdoc.dept.service.DeptService;
import com.e3.basdoc.region.model.RegionModel;
import com.e3.basdoc.region.server.RegionServiceClient;
import com.e3.basdoc.region.service.RegionQueryService;
import com.e3.core.base.ServiceException;
import com.e3.core.base.ServiceLocator;
import com.e3.web.core.client.WebException;
import com.e3.web.core.server.GwtRPCAction;
import com.e3.web.core.server.GwtRPCExecutor;
import com.e3.web.core.server.GwtRPCVoidExecutor;

public class DeptRPCServiceImpl extends GwtRPCAction implements DeptRPCService {

	public DeptModel newDept(final DeptModel deptModel) throws WebException {
		return new GwtRPCExecutor<DeptModel>() {

			@Override
			public DeptModel execute() throws ServiceException, Exception {
				return DeptServiceClient.getDeptService().newDept(deptModel);
			}

		}.doExecute();
	}

	public List<DeptModel> getDeptsByCondition(final DeptQC deptQC) throws WebException {
		return new GwtRPCExecutor<List<DeptModel>>() {

			@Override
			public List<DeptModel> execute() throws ServiceException, Exception {
				List<DeptModel> depts = DeptServiceClient.getDeptQueryService().getDeptByCondition(deptQC);
				if (depts == null || depts.size() <= 0) {
					return depts;
				}
				fillRegion(depts);
				fillParentDept(depts);
				return depts;
			}

		}.doExecute();
	}

	public DeptModel modifyDept(final DeptModel deptModel) throws WebException {
		return new GwtRPCExecutor<DeptModel>() {

			@Override
			public DeptModel execute() throws ServiceException, Exception {
				return DeptServiceClient.getDeptService().modifyDept(deptModel);
			}

		}.doExecute();
	}

	public void deleteDepts(final List<DeptModel> deptModels) throws WebException {
		new GwtRPCVoidExecutor() {

			@Override
			public void execute() throws ServiceException, Exception {
				DeptServiceClient.getDeptService().deleteDepts(deptModels);
			}

		}.doExecute();
	}

	public List<DeptModel> getDeptChildren(final DeptModel deptModel) throws WebException {
		return new GwtRPCExecutor<List<DeptModel>>() {

			@Override
			public List<DeptModel> execute() throws ServiceException, Exception {
				List<DeptModel> models = new ArrayList<DeptModel>();
				if (deptModel == null) {
					DeptModel model = DeptHelper.INSTANCE.createRootModel();
					models.add(model);
					return models;
				}

				DeptModel qModel = deptModel;
				if (DeptHelper.INSTANCE.isRootModel(qModel)) {
					qModel = null;
				}

				List<DeptModel> result = DeptServiceClient.getDeptQueryService().getDeptChildren(qModel);
				DeptHelper.INSTANCE.setDeptRelations(deptModel, result);
				fillRegion(result);
				return result;
			}

		}.doExecute();
	}

	public DeptModel getDeptById(final String id) throws WebException {
		return new GwtRPCExecutor<DeptModel>() {

			@Override
			public DeptModel execute() throws ServiceException, Exception {
				return DeptServiceClient.getDeptQueryService().getDeptModelById(Long.parseLong(id));
			}

		}.doExecute();
	}

	private void fillRegion(List<DeptModel> depts) throws Exception {
		if(depts==null||depts.isEmpty()){
			return;
		}
		// 填充dept的所在地区
		List<Long> regionIds = new ArrayList<Long>(depts.size());
		for (DeptModel dept : depts) {
			Long regionId = dept.getRegionId();
			if (!regionIds.contains(regionId)) {
				regionIds.add(regionId);
			}
		}
		if (!regionIds.isEmpty()) {
			Long[] ids = new Long[regionIds.size()];
			regionIds.toArray(ids);
			List<RegionModel> regions = RegionServiceClient.getRegionQueryService().getRegionModelByIds(ids);
			for (DeptModel dept : depts) {
				Long regionId = dept.getRegionId();
				for (RegionModel region : regions) {
					Long id = region.getId();
					if (regionId.equals(id)) {
						dept.setRegion(region);
						break;
					}
				}
			}
		}
	}

	private void fillParentDept(List<DeptModel> depts) throws Exception {
		// 填充dept的上级dept
		List<Long> parentIds = new ArrayList<Long>(depts.size());
		for (DeptModel dept : depts) {
			if (!DeptHelper.INSTANCE.isTopModel(dept)) {
				Long parentId = dept.getParentId();
				if (!parentIds.contains(parentId)) {
					parentIds.add(parentId);
				}
			}
		}
		if (!parentIds.isEmpty()) {
			Long[] ids = new Long[parentIds.size()];
			parentIds.toArray(ids);
			List<DeptModel> parentDepts = DeptServiceClient.getDeptQueryService().getDeptModelByIds(ids);
			for (DeptModel dept : depts) {
				Long pID = dept.getParentId();
				for (DeptModel deptModel : parentDepts) {
					Long id = deptModel.getId();
					if (pID.equals(id)) {
						dept.setParentDept(deptModel);
						break;
					}
				}
			}
		}
	}

}
